package cn.imnu.legalaid.controller;

import cn.imnu.legalaid.dto.User.PublicUserInfoDTO;
import cn.imnu.legalaid.service.UserService;
import cn.imnu.legalaid.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/public/users")
public class PublicUserController {

    @Autowired
    private UserService userService; // 直接使用现有的UserService

    /**
     * 获取公开用户信息（通过ID）
     */
    @GetMapping("/{id}")
    public Result<PublicUserInfoDTO> getPublicUserInfo(@PathVariable Integer id) {
        try {
            PublicUserInfoDTO userInfo = userService.getPublicUserInfo(id);
            if (userInfo == null) {
                return Result.error("用户不存在");
            }
            return Result.success(userInfo, "获取用户信息成功");
        } catch (Exception e) {
            log.error("获取公开用户信息失败", e);
            return Result.error("获取用户信息失败");
        }
    }

    /**
     * 批量获取公开用户信息
     */
    @PostMapping("/batch")
    public Result<List<PublicUserInfoDTO>> getPublicUserInfoBatch(@RequestBody List<Integer> userIds) {
        try {
            List<PublicUserInfoDTO> userInfoList = userService.getPublicUserInfoBatch(userIds);
            return Result.success(userInfoList, "批量获取用户信息成功");
        } catch (Exception e) {
            log.error("批量获取公开用户信息失败", e);
            return Result.error("批量获取用户信息失败");
        }
    }

    /**
     * 获取律师列表的公开信息
     */
    @GetMapping("/lawyers")
    public Result<List<PublicUserInfoDTO>> getPublicLawyersInfo(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String specialty) {
        try {
            List<PublicUserInfoDTO> lawyers = userService.getPublicLawyersInfo(page, size, specialty);
            return Result.success(lawyers, "获取律师列表成功");
        } catch (Exception e) {
            log.error("获取律师公开信息失败", e);
            return Result.error("获取律师信息失败");
        }
    }
}
